package org.testmyapp.tasks;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public abstract class GeneralUI {

	public GeneralUI() {
		super();
	}

	protected Map<String, Object> vars = new HashMap<String, Object>();

	protected String initial = "INITIAL";
	public static final String PAGE_CHANGE = "ChangePage";
	public static final String COMPLETE_TASK = "CompleteTask";

	public String getFormularHTMLData(String formname, String action) {
		if (formname == null)
			return null;
		Class<FormularCollection> clazz = FormularCollection.class;
		Method[] methods = clazz.getDeclaredMethods();
		for (Method m : methods) {
			if (m.getAnnotations() == null || m.getAnnotations().length == 0)
				continue;
			Annotation annotation = m.getAnnotations()[0];
			if (annotation instanceof Formular) {
				if (formname.equals(((Formular) annotation).name())) {
					try {
						return (String) m.invoke(clazz, vars, action);
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return null;
	}

}